package com.xxx.dorm.dao.impl;

import com.xxx.dorm.dao.RecordDao;
import com.xxx.dorm.domain.Record;
import com.xxx.dorm.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RecordDaoImpl implements RecordDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 查找
     * @return
     */
    @Override
    public List<Record> findAllRecord() {
        String sql = "select * from t_record order by date";
        return template.query(sql,new BeanPropertyRowMapper<>(Record.class));
    }

    @Override
    public void activeDisabled(Integer rid) {
        String sql = "update tb_record set disabled = ? where id = ? ";
        template.update(sql,0,rid);
    }

    /**
     * 增加
     * @param record
     */
    @Override
    public void addRecord(Record record) {
        String sql = "insert into t_record(date,remark,sid) values(?,?,?)";
        template.update(sql,record.getDate(),record.getRemark(),record.getSid());
    }

    /**
     * 通过id查找
     * @param rid
     * @return
     */
    @Override
    public Record findById(Integer rid) {
        String sql = "select * from t_record where id = ? ";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Record.class),rid);
    }

    /**
     * 修改
     * @param record
     */
    @Override
    public void updateRecord(Record record) {
        String sql = "update t_record set sid = ?,date = ?,remark = ? where id = ?";
        template.update(sql,record.getSid(),record.getDate(),record.getRemark(),record.getId());
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteRecord(Integer id) {
        String sql = "delete from t_record where id =?";
        template.update(sql,id);
    }

    /**
     * 通过条件查找
     * @param conditions
     * @param information
     * @return
     */
    @Override
    public List<Record> find(String conditions, String information) {
        String sql = null;
        if(conditions.equals("stuCode")){
            sql = "select t_record.id,t_record.date,t_record.remark,t_record.sid from t_record,t_student where stuCode = ? and t_student.id = t_record.sid";
        }else if(conditions.equals("name")){
            sql = "select t_record.id,t_record.date,t_record.remark,t_record.sid from t_record,t_student where name = ? and t_student.id = t_record.sid";
        }else if(conditions.equals("buildingname")){
            sql = "select t_record.id,t_record.date,t_record.remark,t_record.sid from t_record,t_student,t_building,t_dormitory where t_building.id = t_student.did = t_dormitory.id and t_building.name = ? and t_student.id = t_record.sid and t_building.id = t_dormitory.bid";
        }else if(conditions.equals("sid")){
            sql = "select t_record.id,t_record.date,t_record.remark,t_record.sid from t_record where sid = ?";
        }
        return template.query(sql,new BeanPropertyRowMapper<>(Record.class),information+"");
    }
}
